<?php
$dbh = new PDO('mysql:host=localhost;dbname=blog_development' ,
                 'root' , '' );

function titleValid($title)
{
	return ($title != "" ? true : false); 
}

function bodyValid($body)
{
	return ($body != "" ? true : false); 
}

function insertData($dbh, $title, $body ) 
{		
	
		$sql = "INSERT INTO blogger (title, body)
             		VALUES (".$dbh->quote($title).",".$dbh->quote($body).")";
     			return $dbh->exec($sql);//returns a true or false		
	
}

function getData($dbh)
{
	//print_r($dbh);
	$sql = "SELECT id,title,body from blogger ORDER by id DESC";
	$posts = $dbh->query($sql)->fetchAll();
	return $posts;

	
}

function deleteData($dbh, $id)
{	
	$sql1 = "delete from blogger where id = $id ";
	//$id = '';
	return $dbh->exec($sql1);
	
	
}

function updateData($dbh, $id, $title, $body)
{	
	//print_r("hello: In the update");
	if (!titleValid($title)) 
	{
		//print_r("hello: In the update title");
         	$error = "There is no title. Please enter a title for your comment.";
    	} 
	else if (!bodyValid($dbh, $body)) 
	{
		//print_r("hello: In the update body");
         	$error = "There is no comment. Please enter a comment";
    	}
 	else
	{
		print_r("am I getting Here");
		$sql2 = "UPDATE blogger SET title =\"$title\", body = \"$body\" WHERE id = $id ";
		echo $sql2;
 		//$id = '';
		print_r("the value of id is $id");
		return $dbh->exec($sql2);
		
		
         	$success = "Thank you, The comment has been edited.";

		
    	}
	
}

function goBack()
{
	echo "self.location='index.html'";
}

$error = '' ;
$success = '' ;
$title = isset($_POST['title' ]) ? $_POST['title' ] : '' ;
$body = isset($_POST['body' ]) ? $_POST['body' ] : '' ;
$id = isset($_POST['id' ]) ? $_POST['id' ] : '' ;
$Action= isset($_POST['action']) ? $_POST['action'] : '';
$editableTitle = '';
$editableBody = '';
	if ( $Action == 'delete' )
	{
		deleteData($dbh, $id);
		//$id = '';
	}
	else if( $Action == 'edit' )
	{
		
		//print_r("this will eventually allow you to edit  the comment where id is $id" );	
		$sql3 = "select title from blogger where id=$id";
		//echo "<br>$sql3<br>";
		$editableTitle = $dbh->query($sql3)->fetchAll();

		foreach( $editableTitle as $x)
		{
			$newTitle = htmlentities($x['title'], ENT_QUOTES);
		}
		//echo "editableTitle is ".$newTitle."<br>";
		$sql = "select body from blogger where id=$id";
		$editableBody = $dbh->query($sql)->fetchAll();
		
		foreach( $editableBody as $x)
		{
			$newBody = htmlentities($x['body'], ENT_QUOTES);
		}
		//echo "bodyVal is $newBody<br>";		
		
		
	}

if ($_SERVER['REQUEST_METHOD' ] == 'POST' ) 
{
	print_r("the value of id is $id and I am checking it");
	$sql = " SELECT id FROM blogger ORDER BY id DESC LIMIT 1";
	$count = $dbh->query($sql)->fetchall();
	foreach($count as $y)
	{
		$aCount = htmlentities($y['id'], ENT_QUOTES);
	}
	echo "<br>$aCount is the count<br>";

	if ($id != $aCount)
	{
		if (!titleValid($title)) 
		{
         		$error = "There is no title. Please enter a title for your comment.";
    		} 
		else if (!bodyValid($dbh, $body)) 
		{
         		$error = "There is no comment. Please enter a comment";
    		}
 		else if (insertData($dbh, $title, $body)) 
		{
         		$success = "Thank you, Your comment has been entered.";
    		}		
	}
 	else
	{
   		if (!titleValid($title)) 
		{
			//print_r("hello: In the update title");
         		$error = "There is no title. Please enter a title for your comment.";
    		} 
		else if (!bodyValid($dbh, $body)) 
		{
			//print_r("hello: In the update body");
         		$error = "There is no comment. Please enter a comment";
    		}
 		else
		{
			print_r("am I getting Here");
			$sql2 = "UPDATE blogger SET title =\"$title\", body = \"$body\" WHERE id = $id ";
			//echo $sql2;

			
			$something = $dbh->exec($sql2);
		
		
         		$success = "Thank you, The comment has been edited.";
			 			$id = -1;
		
    		}
		
	}
		
		print_r("the value of id is $id");

     	
}

unset($dbh);
$dbh = new PDO('mysql:host=localhost;dbname=blog_development' ,
                 'root' , '' );

$posts = getData($dbh);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<center>

   		<head><title>Eric Johnson's Blog</title>
		<script language='JavaScript' src='blog.js'></script>	
		</head>
   		<body>
		<h1>Welcome to Eric Johnson's Blog</h1>
		<hr>
		<h2>Post a Comment!</h2>
    			
    		<form name='editComment' method='post' action='/myblog.php'>

		<table>				
						<tr><th colspan = '2' ><font color="blue">Give Us Your Thoughts</th></tr>
						<div style='color: red'>
       						<?php echo htmlentities($error, ENT_QUOTES); ?>
    						</div>
    						<div style='color: green'>
        					<?php echo htmlentities($success, ENT_QUOTES); ?>
     						</div>
						<tr><td>Title:<input type='text' name='title' value ="<?php echo $newTitle; ?>" size='35' maxlength = '30'></td></tr>
						<tr><td><textarea name='body' cols="40" rows="7"><?php echo $newBody; ?></textarea></td></tr>

						
					</table>
				
					<p>

					<input type='submit' value='Submit'>
					&nbsp;&nbsp;<input type='button' value='Back To HomePage' onclick='goBack()'>
					<?php  echo"<input type='hidden' name='id' value='".$id."'>"; ?>
					&nbsp;&nbsp;<input type='reset' value='Clear'>
					<br><br>
					<h3><font color='red'>Please keep all comments as brief as possible.</font></h3>
    		</form>
	
	<hr>
	<hr>
 	<h2>View the Comment(s)!</h2>
	<hr>
     	<div>
		<?php 
			
			foreach($posts as $comment)
			{
				echo "<form method='post' action='/myblog.php'>";
				echo 'Title: ';  echo htmlentities($comment['title'],ENT_QUOTES);
				echo '<br />';
				echo htmlentities($comment['body'],ENT_QUOTES);
				echo '<br />';
				$id = $comment['id'];
				echo "Edit: <input type='radio' name='action' value='edit' checked>"; 
				echo "&nbsp; &nbsp; &nbsp;";
				echo "Delete: <input type='radio' name='action' value='delete'>";
				echo '&nbsp; &nbsp; &nbsp;';
				echo "<input type='submit' value='Submit'/>";
				echo "<input type='hidden' name='id' value='".$id."'>";
				//echo $id;
				echo "</form>";
				echo '<hr>';
			}
		?>
	</div>
	
    	</body>
	</center>
</html>


